// 防抖
export const debounce = (func, wait = 3000, immediate = true) => {
  let timeout = null;
  return function() {
    let context = this;
    let args = arguments;
    if (timeout) clearTimeout(timeout);
    if (immediate) {
      var callNow = !timeout; //点击第一次为true 时间内点击第二次为false 时间结束则重复第一次
      timeout = setTimeout(() => {
        timeout = null;
      }, wait); //定时器ID
      if (callNow) func.apply(context, args);
    } else {
      timeout = setTimeout(function() {
        func.apply(context, args);
      }, wait);
    }
  };
};
  // 时间戳方案
export const throttleTime = (fn, wait = 2000) => {
  var pre = Date.now();
  return function() {
    var context = this;
    var args = arguments;
    var now = Date.now();
    if (now - pre >= wait) {
      fn.apply(context, args);
      pre = Date.now();
    }
  };
};
  // 定时器方案
export const throttleSetTimeout = (fn, wait = 3000) => {
  var timer = null;
  return function() {
    var context = this;
    var args = arguments;
    if (!timer) {
      timer = setTimeout(function() {
        fn.apply(context, args);
        timer = null;
      }, wait);
    }
  };
};

export const debounceEnd = (fn, time) => {
  console.log(fn);
  let timer = null;
  return function () {
    var context = this;
    var args = arguments;
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
    timer = setTimeout(() => {
      // handle();
      fn.apply(context, args);
    }, time);
  };
};